import { debounce } from "@/app/utils";
import { content, customization, personalDetails } from "./initialState";
import { createWithEqualityFn } from "zustand/traditional";
import { produce } from "immer";

export type ResumesContentState = { content: ResumesContent };

export type ResumesContentActions = {
  update: (content: ResumesContent) => void;
  updateSection?: (content: Content, type: Sections) => void;
  addContent?: (
    content: Content,
    type: Sections,
    customization: Customization,
    updateSectionOrder,
    contentKeys
  ) => void;
  deleteContent?: (type: Sections) => void;
};

export type ResumesContentStore = ResumesContentState & ResumesContentActions;

export const defaultInitState: ResumesContent = content;

export const createContentStore = (initState = defaultInitState) => {
  return createWithEqualityFn<ResumesContentStore>()((set, get) => ({
    content: initState,
    update: (data: ResumesContent) => {
      set(() => ({ content: data }));
    },
    updateSection: (data, type) => {
      set((state) => ({
        content: {
          ...state.content,
          [type]: data,
        },
      }));
    },
    addContent: (
      data: Content,
      type: Sections,
      customization: Customization,
      updateSectionOrder: (data: SectionOrder) => void,
      content: ResumesContent
    ) => {
      const contentKeys = Object.keys(content);
      const newCustomization = produce(customization, (draft) => {
        const { one, two } = draft.sectionOrder;
        const needInsertSection = type;
        if (needInsertSection) {
          const onekeys = one.leftSectionChildren.filter((v) =>
            contentKeys.includes(v)
          );
          one.leftSectionChildren = [
            ...new Set([
              ...onekeys,
              needInsertSection,
              ...one.leftSectionChildren,
            ]),
          ];
          if (two.leftSectionChildren.includes(needInsertSection)) {
            const two_l_keys = two.leftSectionChildren.filter((v) =>
              contentKeys.includes(v)
            );
            two.leftSectionChildren = [
              ...new Set([
                ...two_l_keys,
                needInsertSection,
                ...two.leftSectionChildren,
              ]),
            ];
          }
          if (two.rightSectionChildren.includes(needInsertSection)) {
            const two_r_keys = two.rightSectionChildren.filter((v) =>
              contentKeys.includes(v)
            );
            two.rightSectionChildren = [
              ...new Set([
                ...two_r_keys,
                needInsertSection,
                ...two.rightSectionChildren,
              ]),
            ];
          }
          if (
            !two.leftSectionChildren.includes(needInsertSection) &&
            !two.rightSectionChildren.includes(needInsertSection)
          ) {
            if (draft.layout.detailsPosition === "left") {
              two.rightSectionChildren.push(needInsertSection);
            } else {
              two.leftSectionChildren.push(needInsertSection);
            }
          }
        }
      });
      updateSectionOrder(newCustomization.sectionOrder);
      set((state) => ({
        content: {
          ...state.content,
          [type]: data,
        },
      }));
    },
    deleteContent: (type: Sections) => {
      set((state) => {
        const content = { ...state.content };
        delete content[type];
        return {
          content,
        };
      });
    },
  }));
};
